<?php
require( 'mofachi.UserData.php' );
class User {
	private $userData;
	private $is_loaded;
	public function __get( $key ) {
		return $this->userData->$key;
	}
	public function __set( $key, $value ) {
		switch( $key ) {
			case "user_id":
				if( !$this->is_loaded ) {
					$this->userData->user_id = $value;
					break;
				}
			case "reg_time":
			case "edit_time":
				echo "--cannot set ".$key;
				break;
			default:
				$this->userData->$key = $value;
		}
	}
	public function __construct() {
		$this->userData = new UserData();
		$this->is_loaded = false;
	}
	public function isLoaded() {
		return $this->is_loaded;
	}
	public function add() {
		if( !$this->email || !$this->password ) {
			return false;
		}
		if( !get_magic_quotes_gpc() ) {
			$this->email = addslashes( $this->email );
			$this->password = addslashes( $this->password );
		}
		return $this->userData->add();
	}
	public function addInfo() {
		if( !$this->user_id || !$this->nickname ) {
			return false;
		}	
		if ( !get_magic_quotes_gpc() ) {
			$this->user_id = addslashes( $this->user_id );
			$this->nickname = addslashes( $this->nickname );
			$this->realname = addslashes( $this->realname );
			$this->gender = addslashes( $this->gender );
			$this->location_id = addslashes( $this->location_id );
			$this->birthday = addslashes( $this->birthday );
		}
		return $this->userData->addInfo();
	}
	public function loadByID() {
		if( !$this->user_id ) {
			return false;
		}
		$this->is_loaded = $this->userData->loadByID();
		return $this->is_loaded;
	}
	public function loadInfoByID() {
		if( !$this->user_id ) {
			return false;
		}
		$this->is_loaded = $this->userData->loadInfoByID();
		return $this->is_loaded;
	}
	public function loadByEmail() {
		if( !$this->email ) {
			return false;
		}
		$this->is_loaded = $this->userData->loadByEmail();
		return $this->is_loaded;
	}
	public function loadInfoByEmail() {
		if( !$this->email ) {
			return false;
		}
		$this->is_loaded = $this->userData->loadInfoByEmail();
		return $this->is_loaded;
	}
	public function loadInfoByNickname() {
		if( !$this->nickname ) {
			return false;
		}
		$this->is_loaded = $this->userData->loadInfoByNickname();
		return $this->is_loaded;
	}
	public function update() {
		if( !$this->email || !$this->password ) {
			return false;
		}
		if( !get_magic_quotes_gpc() ) {
			$this->email = addslashes( $this->email );
			$this->password = addslashes( $this->password );
		}
		return $this->userData->update();
	}
	public function updateInfo() {
		if( !$this->user_id || !$this->nickname ) {
			return false;
		}	
		if ( !get_magic_quotes_gpc() ) {
			//$this->user_id = addslashes( $this->user_id );
			$this->nickname = addslashes( $this->nickname );
			$this->realname = addslashes( $this->realname );
			$this->gender = addslashes( $this->gender );
			$this->location_id = addslashes( $this->location_id );
			$this->birthday = addslashes( $this->birthday );
		}
		return $this->userData->updateInfo();
	}
	public function remove() {
		if( !$this->user_id ) {
			return false;
		}
		$result = $this->userData->remove();
		if( $result )
			unset( $this->userData );
		return $result;
	}
	public function getBabies() {
		$babies = $this->userData->getBabies();
		$num = count( $babies );
		$result = array();
		require_once( 'mofachi.Baby.php' );
		for( $i = 0; $i < $num; $i++ ) {
			$baby = new Baby();
			$baby->baby_id = $babies[ $i ][ "baby_id" ];
			$baby->loadByID();
			$result[ $i ] = $baby;
		}
		return $result;
	}
	//-------------------------------------------------------------------------------------
	// static functions
	//-------------------------------------------------------------------------------------
	public static function authenticate( $email, $password ) {
		return UserData::authenticate( $email, $password );
	}
	public static function listUser( $offset, $length ) {
		$offset = ( isset( $offset ) ) ? $offset : 0;
		$length = ( isset( $length ) ) ? $length : 20;

		$userData = UserData::listUser( $offset, $length );
		$returnUsers = array();
		foreach( $userData as $ud ) {
			$u = new User();
			$u->userData = $ud;
			array_push( $returnUsers, $u );
		}
		return $returnUsers;
	}
	public static function listUserInfo( $offset, $length ) {
		$offset = ( isset( $offset ) ) ? $offset : 0;
		$length = ( isset( $length ) ) ? $length : 20;

		$userData = UserData::listUserInfo( $offset, $length );
		$returnUsers = array();
		foreach( $userData as $ud ) {
			$u = new User();
			$u->userData = $ud;
			array_push( $returnUsers, $u );
		}
		return $returnUsers;
	}
}
?>